﻿using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using MetaSharp.Transformation.Patterns;

namespace MetaSharp.Transformation
{
    public sealed class Memo
    {
        private IMatch _match;
        private RulePattern _origin;
        private IImmutableArray<Memo> _references;

        public IMatch Match
        {
            get { return _match; }
        }

        public RulePattern Origin
        {
            get { return _origin; }
        }

        public IImmutableArray<Memo> References
        {
            get { return _references; }
        }

        public Memo(IMatch match, RulePattern origin, IImmutableArray<Memo> references)
        {
            _match = match;
            _origin = origin;
            _references = references;
        }

        public override string ToString()
        {
            var refs = new string[this.References.Length];
            var atts = new string[this.Origin.Attributes.Length];
            for (var i = 0; i < this.References.Length; i++)
                refs[i] = this.References[i].ToString();
            for (var i = 0; i < this.Origin.Attributes.Length; i++)
                atts[i] = this.Origin.Attributes[i].ToString();
            return string.Format(
                CultureInfo.InvariantCulture,
                "Memo {{ Attributes=[{0}], Match={1}, References=[{2}] }}",
                string.Join(",", atts),
                this.Match.Value != null ? this.Match.Value : "null",
                string.Join(",", refs));
        }
    }
}
